﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel.DataAnnotations;

namespace Sparkle.UI
{
    public class SparkleValidationRule : ValidationRule
    {
        private ValidationAttribute ValidationRule;

        public SparkleValidationRule(ValidationAttribute validationRule)
        {
            this.ValidationRule = validationRule;
            this.ValidatesOnTargetUpdated = true;
        }

        public override ValidationResult Validate(object value,
                        System.Globalization.CultureInfo cultureInfo)
        {
            bool isValid = false;

            string errorMessage = "";

            try
            {
                ValidationRule.Validate(value, string.Empty);
                isValid = true;
            }
            catch (Exception ex)
            {
                isValid = false;
                errorMessage = ex.Message;
            }
            ValidationResult result = new ValidationResult(isValid, errorMessage);
            return result;
        }
    }
}
